<template>
  <div class="login">
    <LoginHeader>
      <el-form
        slot="container"
        :rules="rules"
        :model="ruleForm"
        label-position="left"
        label-width="0px"
        ref="ruleForm"
      >
        <div class="title">
          <h3>账号密码登录</h3>
        </div>
        <!-- username -->
        <el-form-item prop="username">
          <el-input type="text" v-model="ruleForm.username" placeholder="账号" auto-complete="off">
            <i slot="prefix" class="fa fa-user-o"></i>
          </el-input>
        </el-form-item>
        <!-- password -->
        <el-form-item prop="pwd">
          <el-input type="password" v-model="ruleForm.pwd" placeholder="密码" auto-complete="off">
            <i slot="prefix" class="fa fa-lock"></i>
          </el-input>
        </el-form-item>
        <!-- 登录button -->
        <el-form-item>
          <el-button
            :loading="isLogin"
            @click.native.prevent="handleSubmit"
            type="primary"
            style="width:100%"
          >登录</el-button>
        </el-form-item>
        <!-- 7天登录和忘记密码 -->
        <el-form-item>
          <el-checkbox v-model="ruleForm.autoLogin" :checked="ruleForm.autoLogin">7天内自动登录</el-checkbox>
          <el-button @click="$router.push('/password')" type="text" class="forget">忘记密码</el-button>
        </el-form-item>
      </el-form>
    </LoginHeader>
  </div>
</template>

<script lang="ts">
// 装饰器
import { Vue, Component, Provide } from "vue-property-decorator";
import LoginHeader from "./LoginHeader.vue";
import { getLogin } from "@/utils/login";
@Component({
  components: {
    LoginHeader
  }
})
export default class Login extends Vue {
  //ruleForm 为装饰器的内容,用provide(装饰器)代替vue2.0data
  @Provide() isLogin: boolean = false;
  @Provide() ruleForm: {
    //声明数值的类型
    username: String;
    pwd: String;
    autoLogin: boolean;
  } = {
    //值
    username: "",
    pwd: "",
    autoLogin: true //是否自动登录
  };
  @Provide() rules = {
    username: [{ required: true, message: "请输入账号", trigger: "blur" }],
    pwd: [{ required: true, message: "请输入密码", trigger: "blur" }]
  };
  // void 为没有返回值
  handleSubmit(): void {
    (this.$refs["ruleForm"] as any).validate((valid: boolean) => {
      if (valid) {
        this.isLogin = true;
        getLogin((this as any).ruleForm)
          .then((res: any) => {
            this.isLogin = false;
            // 存储token
            localStorage.setItem("tsToken", res.data.token);
            console.log(res);
          })
          .catch(e => {
            this.isLogin = false;
          });
        // (this as any).$axios
        //   .post("/api/users/login", this.ruleForm)
        //   .then((res: any) => {
        //     this.isLogin = false;
        //     console.log(res);
        //     // 存储token
        //     localStorage.setItem("tsToken", res.data.token);
        //   })
        //   .catch(() => {
        //     this.isLogin = false;
        //   });
      } else {
        console.log("error submit!!");
        return false;
      }
    });
  }
}
</script>
<style lang="scss" scoped>
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

i {
  font-size: 14px;
  margin-left: 8px;
}
.forget {
  float: right;
}
</style>